<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="判定依据" prop="remarkSpare">
                <el-select  v-model="queryParams.remarkSpare" filterable placeholder="选择判定依据" style="width: 100%">
                  <el-option
                    v-for="content in basisList"
                    :key="content.id+''"
                    :label="content.basis"
                    :value="content.id+''">
                  </el-option>
                </el-select>
      </el-form-item>
      <el-form-item label="排查内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入排查内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['risk:content:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['risk:content:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['risk:content:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--		  :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['risk:content:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" width="50" align="center" >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="判定依据" align="center" prop="remarkSpare" :formatter="remarkSpareFormat"/>
      <el-table-column label="排查内容" align="center" prop="content" />
      <el-table-column label="排序" align="center" prop="sort" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="备用1" align="center" prop="remarkSpare1" />-->
<!--      <el-table-column label="备用2" align="center" prop="remarkSpare2" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['risk:content:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['risk:content:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排查内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="判定依据" prop="remarkSpare">
        <el-select  v-model="form.remarkSpare" filterable placeholder="选择判定依据" style="width: 100%">
          <el-option
            v-for="content in basisList"
            :key="content.id+''"
            :label="content.basis"
            :value="content.id+''">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="排查内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" placeholder="请输入排序" style="width: 100%"/>
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用1" prop="remarkSpare1">-->
<!--          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="remarkSpare2">-->
<!--          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContent, getContent, delContent, addContent, updateContent, exportContent } from "@/api/risk/content";
import { listOption } from "@/api/risk/basis";


export default {
  name: "Content",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排查内容表格数据
      contentList: [],
      basisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 主键ID字典
      idOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remarkSpare: [
          { required: true, message: "判定依据不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "排查内容不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // this.getList();
    this.getBasis();
  },
  watch: {
    '$route': {
      handler: function (to, from) {
        // 当路由发生变化时会调用这个方法
        // 你可以在这里处理参数变化
        if(to.params.remarkSpare){
          console.log('参数变化了:', to.params.remarkSpare);
          const remarkSpare = to.params.remarkSpare;
          if (remarkSpare) {
            this.queryParams.remarkSpare = remarkSpare + '';
          }
          this.getList();
        }
      },
      // 如果需要深度监听路由参数，可以设置deep: true
      deep: true
    }
  },
  mounted() {
    // 获取传递的参数
    const remarkSpare = this.$route.params.remarkSpare;
    console.log('参数:', remarkSpare);
    if (remarkSpare) {
      this.queryParams.remarkSpare = remarkSpare + '';
    }
    this.getList();
  },
  methods: {
    /** 查询排查内容列表 */
    getList() {
      this.loading = true;
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getBasis() {
      listOption().then(response => {
          this.basisList = response.data;
        }
      );
      console.log(this.basisList)
    },
    // 主键ID字典翻译
    remarkSpareFormat(row, column) {
      const dict = this.basisList.find(dict => (dict.id+"") === row.remarkSpare);

      return dict ? dict.basis : '';
      // return this.selectDictLabel(this.cOptions, row.c);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
        sort: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null,
        deptId: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排查内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排查内容";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContent(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除选择的排查内容数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delContent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有排查内容数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportContent(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
